package niumao.minecraft.uhc.listener;

import niumao.minecraft.uhc.Util;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;

public class scythe implements Listener {
    @EventHandler
    public void on(EntityDamageByEntityEvent event) {
        if (event.getDamager() instanceof Player && event.getEntity() instanceof Player) {
            Player attacker = (Player) event.getDamager();
            Player attacked = (Player) event.getEntity();
            ItemStack item = attacker.getInventory().getItemInMainHand();
            ItemMeta itemMeta = item.getItemMeta();
            if (itemMeta == null) return;
            if (itemMeta.getDisplayName().equals(Util.getStr("Scythe"))) {
                Damageable itemDamage = (Damageable) item.getItemMeta();
                if (itemDamage.getDamage() < 250) {
                    attacked.setHealth(attacked.getHealth() - attacked.getHealth() * 0.2D);
                    itemDamage.setDamage(itemDamage.getDamage() + 50);
                    item.setItemMeta((ItemMeta) itemDamage);
                    return;
                }
                attacker.getInventory().remove(Material.IRON_HOE);
            }
        }
    }
}
